package simulation;

import java.util.Date;
import java.util.Random;


public class Car extends Thread{
	
	private int speed;
	private int id;
    private long timeTaken;
    private Road road;
	Random random = new Random();
	int left = 0;
	int right = 1;
	private int segment;
	private int lane;		
	private long waitTime = 1000;
	private int ride = 0; 
	
	public Car(int id,Road road,int lane,int segment,int speed)
	{
		this.id=id;
		this.road=road;
		this.lane=lane;
		this.segment=segment;
		this.speed=speed;
		start();
	}
	
	public void run(){

				timeTravelled(); 
				
               // System.out.println("Initially:Car "+id+" is in ("+lane+","+segment+")");
				
			//	road.setPosition(id,lane, segment);
				synchronized(this){
				Road.roadArray[lane][segment]=1;
				System.out.println("		Initially set:Car "+id+" is in ("+lane+","+segment+")");
				}
				
				
				try {
					Thread.sleep(this.timeTaken);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			while(this.segment<=3){	
				
				synchronized(this){
					nextAvalability(lane,segment);
				}
				
				if(ride == 1){
					try {
						Thread.sleep(this.timeTaken);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
				}
				
		/*		if(road.rightAvailable(id,lane, segment)==true){
					synchronized(this){
					int lane1 = lane;
					lane=right;segment++;
					road.setPosition(id, lane, segment);
					road.resetPosition(id,lane1,segment-1);
					}
					try {
						Thread.sleep(this.timeTaken);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					
				else {
						road.leftAvailable(id,lane,segment);
						synchronized(this){
						int lane2 = lane;
						lane=left;segment++;
						road.setPosition(id, lane, segment);
						road.resetPosition(id,lane2,segment-1);
						}
						try {
							Thread.sleep(this.timeTaken);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
				}*/
				
			  } 
			}
	
	

	private synchronized void nextAvalability(int lane, int segment) {
		
		if(Road.roadArray[1][segment+1]==0){
			Road.roadArray[1][segment+1] =1;
			System.out.println("Car "+id+" SET (1"+","+(segment+1)+")");
			Road.roadArray[lane][segment]=0;
			System.out.println("Car "+id+" RESET ("+lane+","+segment+")");
			this.lane=1;this.segment++;
			System.out.println("		Car "+id+" is in ("+this.lane+","+this.segment+")");
			ride=1;
		}
		else{
			if(Road.roadArray[0][segment+1]==0){
				Road.roadArray[0][segment+1] =1;
				System.out.println("Car "+id+" SET (0"+","+(segment+1)+")");
				Road.roadArray[lane][segment]=0;
				System.out.println("Car "+id+" RESET ("+lane+","+segment+")");
				this.lane=0;this.segment++;
				System.out.println("		Car "+id+" is in ("+this.lane+","+this.segment+")");
				ride=1;
			} 
			else
				try {
					System.out.println("Waiting!:Car "+id+" is in ("+this.lane+","+this.segment+")");
					this.wait();
				} catch (InterruptedException e) { 
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
	}

	/*public void setSpeed() {
		// TODO Auto-generated method stub
		speed = random.nextInt(10);
	}*/

        public int getSpeed(){
            return speed;
        }

        public void timeTravelled(){
            this.timeTaken = (road.blockSize/this.speed)*1000;
        }
}
